﻿using System;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace TShooter.TfsSearch.Engine
{
    public static class Logger
    {
        private const string LOG_NAME = "Application";
        private const string SOURCE_NAME = "TFS Search Engine";
        private static int _EntryID;
        private static readonly EventLog _Log;

        static Logger()
        {
            _Log = new EventLog(LOG_NAME);
            _Log.Source = SOURCE_NAME;
        }

        public static void Write(LogLevel level, LogCategory category, string message)
        {
            Write(level, category, message, Interlocked.Increment(ref _EntryID));
        }

        public static void Write(LogLevel level, LogCategory category, string message, Exception exception)
        {
            Write(level, category, message, Interlocked.Increment(ref _EntryID), exception);
        }

        public static void Write(LogLevel level, LogCategory category, string message, int entryID)
        {
            _Log.WriteEntry(message, (EventLogEntryType) level, entryID, (short) category);
        }

        public static void Write(LogLevel level, LogCategory category, string message, int entryID, Exception exception)
        {
            _Log.WriteEntry(message, (EventLogEntryType) level, entryID, (short) category,
                            Encoding.Default.GetBytes(exception.ToString()));
        }
    }
}